﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using BrokerBase.Lib.Company;
using BrokerBase.Lib.Core;
using Broker.Model;
using BrokerBase.Lib.Client;


namespace Broker
{
    /// <summary>
    /// ConfigPage.xaml 的交互逻辑
    /// </summary>
    public partial class ConfigPage : Page
    {
        private ClientConfiguration model;
        private ConfigForm owner;
        private IList<ICompany> companies;

        public ConfigPage()
        {
            InitializeComponent();
            Loaded += new RoutedEventHandler(ConfigPage_Loaded);
        }

        public ConfigPage(ConfigForm owner)
            : this()
        {
            this.owner = owner;
        }

        void ConfigPage_Loaded(object sender, RoutedEventArgs e)
        {
            LoadCompanies();
            LoadConfigModel();

            DataContext = model;

            if (model.DefaultSex == "男")
                rbtMale.IsChecked = true;
            else
                rbtFmale.IsChecked = true;

        }

        private void LoadCompanies()
        {
            BaseService<Company> svr=new BaseService<Company>(AppGlobal.HbmSessionFactory.GetCurrentSession());

            companies = svr.GetListByHQL("from Company").Cast<ICompany>().ToList<ICompany>();

            ICompany cmp = new Company();
            cmp.Name = "[全部]";
            cmp.Id = 0;

            companies.Insert(0, cmp);

            cmbCompany.ItemsSource = companies.Select(p => p.Name);
        }

        private void LoadConfigModel()
        {
            model = ClientConfiguration.Load(AppGlobal.GetAppPath()+@"\"+ ClientConfiguration.USER_FILE_NAME);
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            if (!Verify())
            {
                return;
            }
            try
            {
                if (rbtMale.IsChecked==true)
                    model.DefaultSex = "男";
                else
                    model.DefaultSex = "女";

                ClientConfiguration.Save(model, AppGlobal.GetAppPath()+@"\"+ ClientConfiguration.USER_FILE_NAME);

                AppGlobal.CurrentCompany = companies.FirstOrDefault(p => p.Name == cmbCompany.SelectedItem as string);
                AppGlobal.ClientCfg = model;

                MessageBox.Show("保存成功", "提示");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private bool Verify()
        {
            if (model.BeforeBirthday > 30)
            {
                MessageBox.Show("生日提醒不能大于30天", "提示");
                return false;
            }

            if (model.BeforePayment > 30)
            {
                MessageBox.Show("付款提醒不能大于30天", "提示");
                return false;
            }

            if (model.MaxAge > 120)
            {
                MessageBox.Show("最大年龄不能超过120岁", "提示");
                return false;
            }

            return true;
        }

        private void btnExit_Click(object sender, RoutedEventArgs e)
        {
            owner.Close();
        }
    }
}
